<template>
  <el-container class="home-container">
    <!-- Header -->
    <el-header>
      <div>
        <img
          src="../assets/static/img/00f7ce062ff37075-52173f9ca1848127-39352c86ea7ffc86a1755ed9ecde7a62.jpg"
        />
        <span>2020年10月31日3:33:18</span>
      </div>

      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- ===========================AsideAside============================ -->
    <el-container>
      <!--页面内容-->

      <el-aside :width="isCollapse ? '64px':'200px'" >

        <!--侧边栏-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>

        <el-menu
          class="el-menu-vertical-demo"
          background-color="#313743"
          text-color="#fff"
          active-text-color="#6AA090"
          unique-opened
          :collapse ='isCollapse'
          :collapse-transition ='false'
          :router="true"
          :default-active="activePath"
        >
          <!--一级菜单-->

          <el-submenu :index="item.id + ''" v-for="item in menuslist" :key="item.id">

          
            <!--一级菜单魔板-->

            <template slot="title">
              <i :class="iconObj[item.id]"></i>

              <span>{{item.authName}}</span>
            </template>

            <el-menu-item :index="'/' + childrenItem.path" v-for="childrenItem in item.children"
             :key="childrenItem.id" @click="saveNatstate('/' + childrenItem.path)">
                <!-- 二级菜单 -->
              <template slot="title">
                <i class="el-icon-menu"></i>

                <span>{{childrenItem.authName}}</span>

              </template>

              
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- Main -->
      <el-main> <router-view></router-view> 
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data(){
    return {
      menuslist:[],
      iconObj:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao',
      },
      isCollapse:false,
      activePath:''
   
    }
  },

  

  created(){
    this.getmenuslist(),
    this.activePath = window.sessionStorage.getItem('activepath')
    
  },

  methods: {
    loginOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },

    // 获取所有的菜单
   async getmenuslist(){
    const {data:res} =  await this.$http.get('menus')
    if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    this.menuslist = res.data;
    // console.log(res);
  
    },
    toggleCollapse(){ //切换Aside展开与折叠
        this.isCollapse = !this.isCollapse;
        console.log(this.isCollapse);
    },
    saveNatstate(activePath){           //保存当前index地址
      window.sessionStorage.setItem('activepath',activePath)
      this.activePath = activePath
      
    }

  },
};
</script>

<style lang="less" scoped>


.el-header {
  padding: 0;
  background-color: #363d40;

  justify-content: space-between;
  align-items: center;
  display: flex;
  color: #ffffff;
  font-size: 20px;
}

.home-container {
  height: 100%;
}

.el-aside {
  background-color: #313743;

  

}
.el-main {
  background-color: #e9edf0;
}
.el-header img {
  height: 50px;
  margin-right: 20px;
}
.el-header div {
  display: flex;
  align-items: center;
}

.iconfont {
  margin-right: 10px;
}

  .el-menu{
    border-right: none;
  }

  .toggle-button{
    background-color:#4a5064;
    font-size: 10px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;

  }
</style>